package com.two.sso.controller;


import com.alibaba.fastjson.JSON;
import com.two.common.entity.Permission;

import com.two.common.entity.User;
import com.two.common.util.CommonResult;
import com.two.sso.feign.PermissionOpenFeign;
import com.two.sso.vo.LoginVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/sso")
public class LoginController {

    // 后台处理类
    @RequestMapping("test")
    public String test(){
        return "测试";
    }
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private PermissionOpenFeign permissionOpenFeign;

    @RequestMapping ("/login")
    public CommonResult login(@RequestBody LoginVo loginVo){
        Subject subject = SecurityUtils.getSubject();
        try{
            UsernamePasswordToken token=new UsernamePasswordToken(loginVo.getUsername(),loginVo.getPassword());
            //交于shiro自定义realm中的认证方法
            subject.login(token);

            //生成token. (1)UUID生成一个唯一id并把该值保存到redis中 [2]通过JWT来生成token.[(1)头信息 (2)载体 (3)密钥]
            String uuid= UUID.randomUUID().toString().replace("-","");

            //登录成功后的用户信息
            User user = (User) subject.getPrincipal();
            System.out.println(user.toString());
            if (user.getRoleid()==3){
                return new CommonResult(4444,"用户权限不足,登陆失败",null);
            }
            user.setPassword(null);
            //根据用户id查询该用户具有的权限。
            List<Permission> permissions=permissionOpenFeign.findPermissionByUserid(user.getUserid());
            List<String> path = new ArrayList<>();
            for (Permission permission : permissions) {
                if (permission!=null&&permission.getPath()!=null && permission.getPath()!=""){
                    path.add(permission.getPath());
                }
                if (permission!=null&&permission.getPasspath()!=null && permission.getPasspath()!=""){
                    path.add(permission.getPasspath());
                }
            }
            //getData():LinkedHashMap
            Map<String,Object> map=new HashMap<>();
            map.put("user",user);
            //map.put("permissions",permissions);
            map.put("permissions",path);
            // 保存在redis中 value应该为包含用户信息以及该用户具有的权限
            redisTemplate.opsForValue().set(uuid,JSON.toJSONString(map),30,TimeUnit.HOURS);
            return new CommonResult(2000,"登录成功",uuid);
        }catch (Exception e){
            e.printStackTrace();
            return new CommonResult(5000,"账号或密码错误",null);
        }
    }


}
